{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sort the People"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sortPeople"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #按身高排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串数组 <code>names</code> ，和一个由 <strong>互不相同</strong> 的正整数组成的数组 <code>heights</code> 。两个数组的长度均为 <code>n</code> 。</p>\n",
    "\n",
    "<p>对于每个下标 <code>i</code>，<code>names[i]</code> 和 <code>heights[i]</code> 表示第 <code>i</code> 个人的名字和身高。</p>\n",
    "\n",
    "<p>请按身高 <strong>降序</strong> 顺序返回对应的名字数组 <code>names</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>names = [\"Mary\",\"John\",\"Emma\"], heights = [180,165,170]\n",
    "<strong>输出：</strong>[\"Mary\",\"Emma\",\"John\"]\n",
    "<strong>解释：</strong>Mary 最高，接着是 Emma 和 John 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>names = [\"Alice\",\"Bob\",\"Bob\"], heights = [155,185,150]\n",
    "<strong>输出：</strong>[\"Bob\",\"Alice\",\"Bob\"]\n",
    "<strong>解释：</strong>第一个 Bob 最高，然后是 Alice 和第二个 Bob 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == names.length == heights.length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>\n",
    "\t<li><code>1 &lt;= names[i].length &lt;= 20</code></li>\n",
    "\t<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>names[i]</code> 由大小写英文字母组成</li>\n",
    "\t<li><code>heights</code> 中的所有值互不相同</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sort-the-people](https://leetcode.cn/problems/sort-the-people/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sort-the-people](https://leetcode.cn/problems/sort-the-people/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"Mary\",\"John\",\"Emma\"]\\n[180,165,170]', '[\"Alice\",\"Bob\",\"Bob\"]\\n[155,185,150]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for _, name in sorted(zip(heights, names), reverse=True)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        ans = [a for _, a in sorted(zip(heights, names))][::-1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        ii = len(names)\n",
    "        j = 1\n",
    "        while j < ii:\n",
    "            for i in range(ii):\n",
    "                if heights[i] < heights[j]:\n",
    "                    a = heights[i]\n",
    "                    heights[i] = heights[j]\n",
    "                    heights[j] = a\n",
    "                    b = names[i]\n",
    "                    names[i] = names[j]\n",
    "                    names[j] = b\n",
    "            j += 1\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        for i in range(len(names)):\n",
    "            highest = i\n",
    "            for j in range(i, len(names)):\n",
    "                if heights[j] > heights[i]:\n",
    "                    highest = j\n",
    "                    heights[highest], heights[i] = heights[i], heights[highest]\n",
    "                    names[highest], names[i] = names[i], names[highest]\n",
    "\n",
    "        return names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n = len(names)\n",
    "        indices = list(range(n))\n",
    "        indices.sort(key=lambda x: heights[x], reverse=True)\n",
    "        res = []\n",
    "        for i in indices:\n",
    "            res.append(names[i])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        for i in range(len(heights)-1):\n",
    "            for j in range(len(heights)-i-1):\n",
    "                if heights[j] < heights[j+1]:\n",
    "                    heights[j], heights[j+1] = heights[j+1], heights[j]\n",
    "                    names[j], names[j+1] = names[j+1], names[j]\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        hhash = dict()\n",
    "        for i in range(len(heights)):\n",
    "            hhash[heights[i]] = i\n",
    "        order = []\n",
    "        for key in sorted(hhash):\n",
    "            order.insert(0,names[hhash[key]])\n",
    "        \n",
    "        return order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n = len(names)\n",
    "        for i in range(n):\n",
    "            for j in range(i, n):\n",
    "                if heights[i] < heights[j]:\n",
    "                    heights[i], heights[j] = heights[j], heights[i]\n",
    "                    names[i], names[j] = names[j], names[i]\n",
    "        return names\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n = len(names)\n",
    "        indices = list(range(n))\n",
    "        indices.sort(key=lambda x: heights[x], reverse=True)\n",
    "        res = []\n",
    "        for i in indices:\n",
    "            res.append(names[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        for i in range(len(heights)-2,-1,-1):\n",
    "            flag=i+1\n",
    "            for j in range(i,-1,-1):\n",
    "                if heights[j]<heights[flag]:\n",
    "                    heights[j], heights[flag] = heights[flag], heights[j]\n",
    "                    names[j],names[flag]=names[flag],names[j]\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#202042120108梁美怡\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for height, name in sorted(zip(heights, names), reverse=True)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        data = dict(zip(heights, names))\n",
    "        heights.sort(reverse = True)\n",
    "        result = []\n",
    "        for ii in heights:\n",
    "            result.append(data[ii])\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        high = sorted(heights, reverse=True)\n",
    "        result = []\n",
    "        for i in range(len(names)):\n",
    "            result.append(names[heights.index(high[i])])\n",
    "\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        length = len(heights)\n",
    "        n = 0\n",
    "        for i in range(length):\n",
    "            for j in range(i+1,length):\n",
    "                if heights[j] > heights[i]:\n",
    "                    temp = heights[i]\n",
    "                    heights[i] = heights[j]\n",
    "                    heights[j] = temp\n",
    "                    str = names[i]\n",
    "                    names[i] = names[j]\n",
    "                    names[j] = str\n",
    "                    \n",
    "\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        a=[]\n",
    "        b=sorted(heights,reverse=True)\n",
    "        for i in b:\n",
    "            a.append(names[heights.index(i)])\n",
    "        return a\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        len_=len(heights)\n",
    "        list_=[]\n",
    "        for i in range(len_):\n",
    "            a=heights.index(max(heights))\n",
    "            list_.append(names[a])\n",
    "            heights[a]=0\n",
    "        return list_\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n=len(names)\n",
    "        for i in range(n-1):\n",
    "            loc=i\n",
    "            for j in range(i+1,n):\n",
    "                if heights[j]>heights[loc]:\n",
    "                    loc=j\n",
    "            heights[i],heights[loc]=heights[loc],heights[i]\n",
    "            names[i],names[loc]=names[loc],names[i]\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        value = {}\n",
    "        for i in range(len(names)):\n",
    "            value[heights[i]] = names[i]\n",
    "\n",
    "        heights.sort(reverse=True)\n",
    "        result = []\n",
    "        for h in heights:\n",
    "            result.append(value[h])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        for i in range(len(heights)-1):\n",
    "            tall = i\n",
    "            for j in range(i, len(heights)):\n",
    "                if heights[j] > heights[tall]:\n",
    "                    tall = j\n",
    "            heights[i], heights[tall] = heights[tall], heights[i]\n",
    "            names[i], names[tall] = names[tall], names[i]\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        studic = dict()\n",
    "        for name,height in zip(names,heights):\n",
    "            studic[height] = name\n",
    "\n",
    "        ans = []\n",
    "        for i in sorted(studic.keys(),reverse = True):\n",
    "            ans.append(studic[i])\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n = len(names)\n",
    "        temp = []\n",
    "        for i in range(n):\n",
    "            temp.append([heights[i], names[i]])\n",
    "        temp.sort()\n",
    "        res = []\n",
    "        for i in range(n-1, -1, -1):\n",
    "            res.append(temp[i][1])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        for i in range(len(names)):\n",
    "            for j in range(1,len(names)-i):\n",
    "                if heights[j-1] > heights[j]:\n",
    "                    names[j-1],names[j] = names[j],names[j-1]\n",
    "                    heights[j-1],heights[j] = heights[j],heights[j-1]\n",
    "        return names[::-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        ans = []\n",
    "        for i in heights:\n",
    "            ans.append(names[heights.index(max(heights))])\n",
    "            heights[heights.index(max(heights))] = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def partition(self,names: List[str], heights: List[int],low: int, high: int) -> int:\n",
    "        base=heights[high]\n",
    "        position=low\n",
    "        for i in range(low,high+1):\n",
    "            if heights[i]>base:\n",
    "                temp1=heights[i]\n",
    "                heights[i]=heights[position]\n",
    "                heights[position]=temp1\n",
    "\n",
    "                temp2=names[i]\n",
    "                names[i]=names[position]\n",
    "                names[position]=temp2\n",
    "\n",
    "                position+=1\n",
    "        temp3=heights[high]\n",
    "        heights[high]=heights[position]\n",
    "        heights[position]=temp3\n",
    "\n",
    "        temp4=names[high]\n",
    "        names[high]=names[position]\n",
    "        names[position]=temp4\n",
    "        return position\n",
    "\n",
    "\n",
    "    def quickSort(self,names: List[str], heights: List[int], low: int, high: int):\n",
    "        if low<high:\n",
    "            position=self.partition(names,heights,low,high)\n",
    "            self.quickSort(names,heights,low,position-1)\n",
    "            self.quickSort(names,heights,position+1,high)\n",
    "    \n",
    "\n",
    "    \n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        self.quickSort(names,heights,0,len(heights)-1)\n",
    "\n",
    "        return names\n",
    "    \n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n = len(names)\n",
    "        for i in range(n-1):\n",
    "            index = i\n",
    "            for j in range(i+1,n):\n",
    "                if heights[index] < heights[j]:\n",
    "                    index = j\n",
    "\n",
    "            names[i],names[index] = names[index],names[i]\n",
    "            heights[i],heights[index] = heights[index],heights[i]\n",
    "\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for _, name in sorted(zip(heights, names), reverse=True)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        for i in range(0, len(names)):\n",
    "            for k in range(i, 0, -1):\n",
    "                if heights[k - 1] < heights[k]:\n",
    "                    temp = heights[k-1]\n",
    "                    heights[k-1] = heights[k]\n",
    "                    heights[k] = temp\n",
    "                    temp = names[k-1]\n",
    "                    names[k-1] = names[k]\n",
    "                    names[k] = temp\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for _, name in sorted(zip(heights, names), reverse=True)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n=sorted({heights[i]:j for i,j in enumerate(names)}.items(),key=lambda x:-x[0])\n",
    "\n",
    "        return [j for i,j in n]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        dictionary = dict(zip(heights, names))\n",
    "        \n",
    "        def findHighest(arr):\n",
    "            highest = arr[0]\n",
    "            highest_index = 0\n",
    "            for i in range(1, len(arr)):\n",
    "                if arr[i] > highest:\n",
    "                    highest = arr[i]\n",
    "                    highest_index = i\n",
    "            return highest_index\n",
    "\n",
    "        new_names = []\n",
    "        for i in range(len(heights)):\n",
    "            highest = findHighest(heights)\n",
    "            new_names.append(dictionary[heights.pop(highest)])\n",
    "        return new_names\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        l = []\n",
    "        m = []\n",
    "        hei = sorted(heights,reverse=True)\n",
    "        for i in hei:\n",
    "            l.append(heights.index(i))\n",
    "        for j in range(len(names)):\n",
    "            m.append(names[l[j]])\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        for i in range(len(heights)):\n",
    "            flag = 0\n",
    "            for j in range(len(heights)-i-1):\n",
    "                if heights[j] > heights[j+1]:\n",
    "                    heights[j],heights[j+1] = heights[j+1],heights[j]\n",
    "                    names[j],names[j+1] = names[j+1],names[j]\n",
    "                    flag = 1\n",
    "            if flag == 0:\n",
    "                break\n",
    "        a = names[::-1]\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n = len(names)\n",
    "        indices = list(range(n))\n",
    "        indices.sort(key=lambda x:heights[x],reverse=True)\n",
    "        res = []\n",
    "        for i in indices:\n",
    "            res.append(names[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        names=zip(names,heights)\n",
    "        _res=sorted(names,key=lambda x:-x[1])\n",
    "        res,_=zip(*_res)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        a = sorted(zip(heights,names),reverse=True)\n",
    "        list = []\n",
    "        for i,j in a:\n",
    "            list.append(j)\n",
    "        return list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        length = len(heights)\n",
    "        for i in range(length):\n",
    "            for j in range(i+1,length):\n",
    "                if heights[j] > heights[i]:\n",
    "                    temp = heights[i]\n",
    "                    heights[i] = heights[j]\n",
    "                    heights[j] = temp\n",
    "                    str = names[i]\n",
    "                    names[i] = names[j]\n",
    "                    names[j] = str\n",
    "                   \n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        dic = dict(zip(heights,names))\n",
    "        heights.sort(reverse=True)\n",
    "        ans = []\n",
    "        for i in heights:\n",
    "            ans.append(dic[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        #冒泡排序\n",
    "        length = len(heights)\n",
    "        for i in range(length-1,-1,-1):\n",
    "            for j in range(i):\n",
    "                if heights[j]>heights[j+1]:\n",
    "                    tmp_height = heights[j]\n",
    "                    tmp_name = names[j]\n",
    "                    heights[j] = heights[j+1]\n",
    "                    names[j] = names[j+1]\n",
    "                    heights[j+1] = tmp_height\n",
    "                    names[j+1] = tmp_name\n",
    "        return names[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        a={}\n",
    "        for i in range(len(names)):\n",
    "            a[heights[i]]=names[i]\n",
    "        t=[]\n",
    "        for i in sorted(heights)[::-1]:\n",
    "            t.append(a[i])\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        n = len(names)\n",
    "        indices = list(range(n))\n",
    "        indices.sort(key=lambda x: heights[x], reverse=True)\n",
    "        res = []\n",
    "        for i in indices:\n",
    "            res.append(names[i])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        t = {}\n",
    "        n = len(names)\n",
    "        for i in range(n):\n",
    "            t[heights[i]] = names[i]\n",
    "        res = []\n",
    "\n",
    "        heights.sort(reverse=True)\n",
    "        for i in range(n):\n",
    "            res.append(t[heights[i]])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        arr = [ (heights[i], names[i]) for i in range(len(names)) ]\n",
    "        return [ name for height, name in sorted(arr, reverse = True) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        length = len(heights)\n",
    "        for i in range(length):\n",
    "            for j in range(i+1,length):\n",
    "                if heights[j] > heights[i]:\n",
    "                    heights[j] = heights[i]^heights[j]\n",
    "                    heights[i] = heights[i]^heights[j]\n",
    "                    heights[j] = heights[i]^heights[j]\n",
    "                    str = names[i]\n",
    "                    names[i] = names[j]\n",
    "                    names[j] = str\n",
    "                   \n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for _, name in sorted(zip(heights, names), reverse=True)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        l = sorted(zip(names, heights), reverse = True, key = itemgetter(1))\n",
    "        return next(zip(*l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for i,name in sorted(zip(heights,names),reverse=True)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for _, name in sorted(zip(heights, names), reverse=True)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return list(map(lambda x: x[1], sorted(zip(heights, names), reverse = True)))\n",
    "        # map(function, iterable可迭代对象)，返回的是一个map对象，以heights排逆序，取names返回形成一个列表返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        people = []\n",
    "\n",
    "        for name, height in zip(names, heights):\n",
    "            people.append((name, height))\n",
    "\n",
    "        people.sort(key=lambda x: x[1], reverse=True)\n",
    "        sorted_name = [name for name, height in people]\n",
    "        return sorted_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for h,name in sorted(zip(heights, names), reverse=True)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        dic = {}\n",
    "        for a,b in zip(heights,names):\n",
    "            dic[a] = b\n",
    "        heights.sort(reverse=True)\n",
    "        for i,num in enumerate(heights):\n",
    "            names[i] = dic[num]\n",
    "        return names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        hash_l = [(x,y) for x,y in zip(names,heights)]\n",
    "        return [i[0] for i in sorted(hash_l,key = lambda x: -x[1])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [i[1] for i in sorted(zip(heights, names), reverse=True)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        print( )\n",
    "        return [ name for name,h in sorted(list(zip(names,heights)),key=lambda x:-x[1])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        result = [(name , heights) for name , heights in zip(names , heights)]\n",
    "        result.sort(key=lambda x : x[1] , reverse = True)\n",
    "        return [name[0] for name in result]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        for i in range(len(names)-1):\n",
    "            exchange = False\n",
    "            for j in range(len(names)-i-1):\n",
    "                if heights[j] < heights[j+1]:\n",
    "                    heights[j], heights[j+1] = heights[j+1], heights[j]\n",
    "                    names[j], names[j+1] = names[j+1], names[j]\n",
    "                    exchange = True\n",
    "            if not exchange:\n",
    "                return names\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for _,name in sorted(zip(heights, names), reverse = True)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        dic = dict(zip(heights,names))\n",
    "        heights.sort(reverse=True)\n",
    "        return [dic[i] for i in heights]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "\n",
    "\n",
    "        data = list(zip(names,heights))\n",
    "\n",
    "\n",
    "        data.sort(key=lambda x : x[1],reverse=True)\n",
    "\n",
    "        res = []\n",
    "\n",
    "\n",
    "        for i in data:\n",
    "\n",
    "            res.append(i[0])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return [name for _, name in sorted(zip(heights, names), reverse=True)]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        dt=list()\n",
    "        l = len(names)\n",
    "        for i in range(l):\n",
    "            dt.append((names[i],heights[i]))\n",
    "        t=sorted(dt,key=lambda k: k[1],reverse=True)\n",
    "        k=list()\n",
    "        for i in t:\n",
    "            k.append(i[0])\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "            lis = sorted(list(dict(zip(heights, range(len(heights)))).items()), key=lambda x: x[0])\n",
    "            return [names[i[1]] for i in lis][::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        hash_l = [(x,y) for x,y in zip(names,heights)]\n",
    "        return [i[0] for i in sorted(hash_l,key = lambda x: -x[1])]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        dic = dict(zip(heights, names))\n",
    "\n",
    "        x = dict(sorted(dic.items(),reverse  =True))\n",
    "        print(x)\n",
    "        return list(x.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        m = len(names)\n",
    "        di = dict()\n",
    "        for i in range(m):\n",
    "            di[heights[i]] = names[i]\n",
    "\n",
    "        res= sorted(di.items(), key=lambda d: d[0],reverse= True)\n",
    "        l = []\n",
    "        for _,y in res:\n",
    "            #print(y)\n",
    "            l.append(y)\n",
    "\n",
    "        return l\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        combined=list(zip(names,heights))\n",
    "        sorted_combined=sorted(combined,key=lambda x:x[1],reverse=True)\n",
    "        res=[name for name,height in sorted_combined]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        coll = [(x,y) for x,y in zip(heights, names)]\n",
    "        names = [name for _, name in sorted(coll, key=lambda x: (-x[0], x[1]))]\n",
    "\n",
    "        return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        \n",
    "        \n",
    "        names_index = [(i,name) for i,name in enumerate(names)]\n",
    "        names_index = sorted(names_index, key= lambda x: -heights[x[0]] )\n",
    "        return [x[1] for x in names_index]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from itertools import zip_longest\n",
    "def sort_people(names: list[str], heights: list[int]) -> list[str]:\n",
    "    return [item[0] for item in sorted(zip_longest(names, heights), key=lambda x: x[1], reverse=True)]\n",
    "    \n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        return sort_people(names, heights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        zipped = zip(names,heights)\n",
    "        zipped_list = sorted(zipped,key=lambda y:y[1],reverse = True)\n",
    "        name_list = []\n",
    "        for i in range(len(zipped_list)):\n",
    "            name_list.append(zipped_list[i][0])\n",
    "        return name_list\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        lst = [(x, y) for x , y in zip(names,heights)]\n",
    "        lst.sort(key=lambda x : x[1],reverse=True)\n",
    "\n",
    "        return [x[0] for x in lst]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        lst = [(names[i], heights[i]) for i in range(len(names))]\n",
    "        lst.sort(key=lambda x: x[1], reverse=True)\n",
    "        return [l[0] for l in lst]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:\n",
    "        arr = zip(names, heights)\n",
    "        arr = sorted(arr, key=lambda x: x[1], reverse=True)\n",
    "        names, heights = zip(*arr)\n",
    "        return names"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
